﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class DuctInsulationManage
	{
		public DuctInsulationManage(UIDocument uiDocument)
		{
			this.uiDoc = uiDocument;
		}

		public string GetDuctSystemName()
		{
			string result;
			try
			{
				Reference reference = this.uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new DuctFilter(), "请选择风管（确定添加绝热层的管道系统）……");
				result = (this.uiDoc.Document.GetElementByRef(reference) as Duct).GetSystemName();
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = "";
			}
			return result;
		}

		public bool AddDuctsInsulationLayer(DuctInsulationType insulationType, List<Duct> listDuct, double calThickness)
		{
			bool result = true;
			try
			{
				if (calThickness <= 0.0)
				{
					result = false;
					return result;
				}
				foreach (Duct duct in listDuct)
				{
					double num = calThickness / 304.8;
					if (duct.GetParameter(BuiltInParameter.RBS_REFERENCE_INSULATION_THICKNESS).AsDouble() <= 0.0)
					{
						DuctInsulation.Create(this.uiDoc.Document, duct.Id, insulationType.Id, num);
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		public bool AddDuctsInsulationLayer(DuctInsulationType insulationType, List<FamilyInstance> listFi, double calThickness)
		{
			bool result = true;
			try
			{
				if (calThickness <= 0.0)
				{
					result = false;
					return result;
				}
				foreach (FamilyInstance familyInstance in listFi)
				{
					if (familyInstance.GetParameter(BuiltInParameter.RBS_REFERENCE_INSULATION_THICKNESS).AsDouble() <= 0.0)
					{
						double num = calThickness / 304.8;
						DuctInsulation.Create(this.uiDoc.Document, familyInstance.Id, insulationType.Id, num);
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		public List<Duct> GetCalcInsulationDucts(string systemName, ref List<FamilyInstance> listFi)
		{
			Selection selection = this.uiDoc.Selection;
			List<Duct> list = new List<Duct>();
			listFi = new List<FamilyInstance>();
			List<Duct> result;
			try
			{
				foreach (Element element in selection.PickElementsByRectangle(new DuctFilterBySystemName(systemName), "请框选需要添加保温层的风管……"))
				{
					if (element is Duct)
					{
						list.Add(element as Duct);
					}
					if (element is FamilyInstance)
					{
						listFi.Add(element as FamilyInstance);
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return result;
		}

		public void CreatInsulationType()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.uiDoc.Document);
			filteredElementCollector.OfClass(typeof(DuctInsulationType));
			List<InsulationLayerParam> allInsulationMaterials = new OperationLocation("").GetAllInsulationMaterials();
			List<Element> list = filteredElementCollector.ToList<Element>();
			if (list == null || list.Count < 1)
			{
				return;
			}
			SubTransaction subTransaction = new SubTransaction(this.uiDoc.Document);
			try
			{
				subTransaction.Start();
				foreach (InsulationLayerParam insulationLayerParam in allInsulationMaterials)
				{
					if (!this.BeExsit(list, insulationLayerParam))
					{
						(list.First<Element>() as DuctInsulationType).Duplicate(insulationLayerParam.Name);
					}
				}
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				subTransaction.RollBack();
			}
		}

		public DuctInsulationType GetInsulationTypeByName(string materialName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.uiDoc.Document);
			filteredElementCollector.OfClass(typeof(DuctInsulationType));
			IEnumerator<Element> enumerator = filteredElementCollector.GetEnumerator();
			enumerator.Reset();
			DuctInsulationType result = null;
			while (enumerator.MoveNext())
			{
				if (enumerator.Current.Name.Equals(materialName))
				{
					result = (enumerator.Current as DuctInsulationType);
				}
			}
			return result;
		}

		private bool BeExsit(List<Element> listType, InsulationLayerParam material)
		{
			bool result = false;
			foreach (Element element in listType)
			{
				if (material.Name.Equals(element.Name))
				{
					result = true;
					break;
				}
			}
			return result;
		}

		private UIDocument uiDoc;
	}
}
